#define _CRT_SECURE_NO_WARNINGS 1
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeZeroSumSublists(ListNode* head)
    {
        ListNode* sen = new ListNode(0, head);
        unordered_map<int, ListNode*> unmap;
        int preFix = 0;
        for (ListNode* cur = sen;cur;cur = cur->next)
        {
            preFix += cur->val;
            unmap[preFix] = cur;
        }
        preFix = 0;
        for (ListNode* cur = sen;cur;cur = cur->next)
        {
            preFix += cur->val;
            cur->next = unmap[preFix]->next;
        }
        return sen->next;
    }
};